package com.huawei.imedia.it.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: tsx
 * @Date: 2021/8/29 - 08 - 29 - 6:55 下午
 * @Description: com.huawei.imedia.it.xueshuxinxi.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("login")
//@CrossOrigin(originPatterns = "*", allowCredentials = "true", maxAge = 3600,allowedHeaders = "*",methods = {})
public class Logincontroller {

    @GetMapping("/session")
    public String login(HttpServletRequest request,HttpSession session){

//        ServletContext servletContext = request.getServletContext();
//        servletContext.setAttribute("username","tsx");
//        HttpSession session = request.getSession();
//        session.setAttribute("username","tsx");
        String id = session.getId();
        String requestURI = request.getRequestURI();
        System.out.println("url:"+requestURI);
        System.out.println("sessionId:"+id);
        System.out.println("username:"+session.getAttribute("username"));
        return  requestURI+id;
    }

    @GetMapping("/remove")
    public String removeSession(HttpSession session, @RequestParam("sid")String id) {
//        session.removeAttribute(id);
        session.invalidate();/
        return "success";
    }

    @GetMapping("/query")
    public String query(HttpSession session,HttpServletRequest request) {
        ServletContext context = session.getServletContext();
        List<String>list = (List)context.getAttribute("onlineUserList");
        for (String name:list) {
            System.out.println(name);
        }
//        return JSONObject.toJSONString(list);
        return (String) session.getAttribute( ");
    }
}
